package it.koutifaris.travianmap.db.impl;

import it.koutifaris.travianmap.db.TMapDomain;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TMapDbOpenHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "travianmap.db";
	private static final int DATABASE_VERSION = 1;

	public TMapDbOpenHelper(Context context) {
		super(context, DB_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(getRevisionsCreationQuery());
		db.execSQL(getAlliancesCreationQuery());
		db.execSQL(getPlayersCreationQuery());
		db.execSQL(getVillagesCreationQuery());
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// NOP for now
	}

	private String getVillagesCreationQuery() {
		return "CREATE TABLE " + TMapDomain.Villages.TABLE_NAME + " ("
				+ TMapDomain.Villages._ID + " INTEGER PRIMARY KEY,"
				+ TMapDomain.Villages.COLUMN_NAME_VILLAGE_ID + " INTEGER,"
				+ TMapDomain.Villages.COLUM_NAME_REVISION_ID + " INTEGER,"
				+ TMapDomain.Villages.COLUMN_NAME_VILLAGE_NAME + " TEXT,"
				+ TMapDomain.Villages.COLUMN_NAME_X + " INTEGER,"
				+ TMapDomain.Villages.COLUMN_NAME_Y + " INTEGER,"
				+ TMapDomain.Villages.COLUMN_NAME_POPULATION + " INTEGER,"
				+ TMapDomain.Villages.COLUMN_NAME_OWNER_ID + " INTEGER"
				+ ");";
	}

	private String getPlayersCreationQuery() {
		return "CREATE TABLE " + TMapDomain.Players.TABLE_NAME + " ("
				+ TMapDomain.Players._ID + " INTEGER PRIMARY KEY,"
				+ TMapDomain.Players.COLUMN_NAME_PLAYER_ID + " INTEGER,"
				+ TMapDomain.Players.COLUM_NAME_REVISION_ID + " INTEGER,"
				+ TMapDomain.Players.COLUMN_NAME_PLAYER_NAME + " TEXT,"
				+ TMapDomain.Players.COLUMN_NAME_TRIBE + " INTEGER,"
				+ TMapDomain.Players.COLUMN_NAME_ALLIANCE_ID + " INTEGER"
				+ ");";
	}

	private String getAlliancesCreationQuery() {
		return "CREATE TABLE " + TMapDomain.Alliances.TABLE_NAME + " ("
				+ TMapDomain.Alliances._ID + " INTEGER PRIMARY KEY,"
				+ TMapDomain.Alliances.COLUMN_NAME_ALLIANCE_ID + " INTEGER,"
				+ TMapDomain.Alliances.COLUM_NAME_REVISION_ID + " INTEGER,"
				+ TMapDomain.Alliances.COLUMN_NAME_ALLIANCE_NAME + " TEXT"
				+ ");";
	}

	private String getRevisionsCreationQuery() {
		return "CREATE TABLE " + TMapDomain.Revisions.TABLE_NAME + " ("
				+ TMapDomain.Revisions._ID + " INTEGER PRIMARY KEY,"
				+ TMapDomain.Revisions.COLUMN_NAME_REVISION_ID + " INTEGER,"
				+ TMapDomain.Revisions.COLUMN_NAME_DATE + " DATE"
				+ ");";
	}
}
